"""
装备和仓库相关模型
"""
from tortoise import fields
from .base import BaseModel


class EquipmentCategory(BaseModel):
    """装备分类模型"""
    
    name = fields.CharField(max_length=100, unique=True, description="分类名称")
    code = fields.CharField(max_length=50, unique=True, description="分类代码")
    description = fields.TextField(null=True, description="分类描述")
    parent = fields.ForeignKeyField(
        "models.EquipmentCategory", 
        related_name="children", 
        null=True, 
        description="父分类"
    )
    
    class Meta:
        table = "equipment_categories"
        table_description = "装备分类表"


class Equipment(BaseModel):
    """装备模型"""
    
    # 基本信息
    name = fields.CharField(max_length=200, description="装备名称")
    code = fields.CharField(max_length=100, unique=True, description="装备编码")
    serial_number = fields.CharField(max_length=100, null=True, description="序列号")
    
    # 分类信息
    category = fields.ForeignKeyField(
        "models.EquipmentCategory", 
        related_name="equipments", 
        description="装备分类"
    )
    brand = fields.CharField(max_length=100, null=True, description="品牌")
    model = fields.CharField(max_length=100, null=True, description="型号")
    
    # 技术规格
    specifications = fields.JSONField(null=True, description="技术规格(JSON)")
    capacity = fields.CharField(max_length=100, null=True, description="容量/载重")
    power_consumption = fields.CharField(max_length=100, null=True, description="功耗")
    
    # 状态信息
    status = fields.CharField(
        max_length=20,
        default="available",
        description="状态: available-可用, in_use-使用中, maintenance-维护中, damaged-损坏, retired-退役"
    )
    condition = fields.CharField(
        max_length=20,
        default="good",
        description="状况: excellent-优秀, good-良好, fair-一般, poor-较差"
    )
    
    # 位置信息
    warehouse = fields.ForeignKeyField(
        "models.Warehouse", 
        related_name="equipments", 
        null=True,
        description="所在仓库"
    )
    location = fields.CharField(max_length=100, null=True, description="具体位置")
    
    # 采购信息
    purchase_date = fields.DateField(null=True, description="采购日期")
    purchase_price = fields.DecimalField(max_digits=12, decimal_places=2, null=True, description="采购价格")
    supplier = fields.CharField(max_length=200, null=True, description="供应商")
    warranty_period = fields.IntField(null=True, description="保修期(月)")
    warranty_expires = fields.DateField(null=True, description="保修到期日")
    
    # 其他信息
    description = fields.TextField(null=True, description="装备描述")
    notes = fields.TextField(null=True, description="备注")
    
    class Meta:
        table = "equipments"
        table_description = "装备表"


class Warehouse(BaseModel):
    """仓库模型"""
    
    # 基本信息
    name = fields.CharField(max_length=200, description="仓库名称")
    code = fields.CharField(max_length=100, unique=True, description="仓库编码")
    warehouse_type = fields.CharField(
        max_length=20,
        default="standard",
        description="仓库类型: standard-标准仓库, cold-冷库, dangerous-危险品仓库, bonded-保税仓库"
    )
    
    # 位置信息
    address = fields.TextField(description="仓库地址")
    latitude = fields.DecimalField(max_digits=10, decimal_places=7, null=True, description="纬度")
    longitude = fields.DecimalField(max_digits=10, decimal_places=7, null=True, description="经度")
    
    # 容量信息
    total_area = fields.DecimalField(max_digits=10, decimal_places=2, null=True, description="总面积(m²)")
    available_area = fields.DecimalField(max_digits=10, decimal_places=2, null=True, description="可用面积(m²)")
    max_capacity = fields.DecimalField(max_digits=12, decimal_places=2, null=True, description="最大容量(吨)")
    current_capacity = fields.DecimalField(max_digits=12, decimal_places=2, default=0, description="当前容量(吨)")
    
    # 管理信息
    manager = fields.ForeignKeyField("models.User", related_name="managed_warehouses", null=True, description="仓库管理员")
    contact_phone = fields.CharField(max_length=20, null=True, description="联系电话")
    
    # 状态信息
    status = fields.CharField(
        max_length=20,
        default="active",
        description="状态: active-活跃, inactive-非活跃, maintenance-维护中"
    )
    
    # 其他信息
    description = fields.TextField(null=True, description="仓库描述")
    
    class Meta:
        table = "warehouses"
        table_description = "仓库表"


class WarehouseZone(BaseModel):
    """仓库区域模型"""
    
    warehouse = fields.ForeignKeyField("models.Warehouse", related_name="zones", description="仓库")
    name = fields.CharField(max_length=100, description="区域名称")
    code = fields.CharField(max_length=50, description="区域编码")
    zone_type = fields.CharField(
        max_length=20,
        default="storage",
        description="区域类型: storage-存储区, loading-装卸区, office-办公区, maintenance-维护区"
    )
    area = fields.DecimalField(max_digits=8, decimal_places=2, null=True, description="区域面积(m²)")
    max_capacity = fields.DecimalField(max_digits=10, decimal_places=2, null=True, description="最大容量(吨)")
    temperature_min = fields.DecimalField(max_digits=5, decimal_places=2, null=True, description="最低温度(°C)")
    temperature_max = fields.DecimalField(max_digits=5, decimal_places=2, null=True, description="最高温度(°C)")
    humidity_min = fields.DecimalField(max_digits=5, decimal_places=2, null=True, description="最低湿度(%)")
    humidity_max = fields.DecimalField(max_digits=5, decimal_places=2, null=True, description="最高湿度(%)")
    
    class Meta:
        table = "warehouse_zones"
        table_description = "仓库区域表"
        unique_together = (("warehouse", "code"),)
